package com.ricchieri.manage.bean;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FlowEvent;

import com.ricchieri.bean.FixedReservationBean;
import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.UserFilter;
import com.ricchieri.lazymodel.UserLazyDataModel;
import com.ricchieri.model.User;
import com.ricchieri.service.interf.IDocumentTypeService;
import com.ricchieri.service.interf.IFixedReservationService;
import com.ricchieri.service.interf.IUserService;
import com.ricchieri.util.FacesUtil;
import com.ricchieri.util.StringUtils;

@ManagedBean(name = "addFixedReservationMB")
@ViewScoped
public class AddFixedReservationManageBean extends CommonFixedReservationManageBean {
	
	private static final long serialVersionUID = 8130921880140230239L;
	
	@ManagedProperty(value = "#{DocumentTypeService}")
	IDocumentTypeService documentTypeService;
	
	@ManagedProperty(value = "#{UserService}")
	IUserService userService;
	
	@ManagedProperty(value = "#{addPassengerReservationMB}")
	AddPassengerReservationManageBean addPassengerReservationManageBean;
	
	@ManagedProperty(value = "#{editPassengerReservationMB}")
	EditPassengerReservationManageBean editPassengerReservationManageBean;
	
	@ManagedProperty(value = "#{FixedReservationService}")
	IFixedReservationService fixedReservationService;
	
	private Map<String, String> trips = null;
	private Map<String, String> reservationType = null;
	private Map<String, String> passengerType = null;
	private Map<String, String> meanPayment = null;
	private Map<String, String> documents = null;
	
	private final String TAB_TRAVEL_INFORMATION = "travelInformation";
	private final String TAB_USER = "user";
	private final String TAB_CONFIRMATION = "confirmation";
	private String actualTab = TAB_TRAVEL_INFORMATION;
	
	private Boolean skipUser = Boolean.FALSE;
	private Boolean skipSchedule = Boolean.FALSE;
	
	private UserFilter userFilter = new UserFilter();
	
	private UserLazyDataModel userLazyDataModel;
	
	@PostConstruct
	public void init() {
		String newReservationOption = (String) FacesUtil.getParametersFlash(BusinessSettings.KEY_ADD_RESERVATION);
		
		if(StringUtils.isNotEmpty(newReservationOption) && newReservationOption.equals(BusinessSettings.KEY_ADD_RESERVATION_USER)) {
			this.setSelectedUser((User) FacesUtil.getParametersFlash(BusinessSettings.KEY_USER));
			skipUser = Boolean.TRUE;
		}
		
		if(StringUtils.isNotEmpty(newReservationOption) && newReservationOption.equals(BusinessSettings.KEY_ADD_RESERVATION_SCHEDULE)) {
			this.setFixedReservationBean((FixedReservationBean) FacesUtil.getParametersFlash(BusinessSettings.KEY_FIX_RESERVATION));
			skipSchedule = Boolean.TRUE;
		}
	}
	
		
	public Map<String, String> getTrips() {
		if (trips == null) {
			trips = getTripService().getComboTrips();
		}

		return trips;
	}
	
	public Map<String, String> getReservationType() {
		if (reservationType == null){
			reservationType=getReservationTypeService().getComboReservationType();
		}
	
		return reservationType;		
	}

	public void setReservationType(Map<String, String> reservationType) {
		this.reservationType = reservationType;
	}
	
	public Map<String, String> getPassengerType() {
		if (passengerType==null){
			passengerType=getPassengerTypeService().getComboPassengerType();
		}
		return passengerType;		
	}

	public void setPassengerType(Map<String, String> passengerType) {
		this.passengerType = passengerType;
	}
	
	public Map<String, String> getMeanPayment() {
		if (meanPayment==null){
			meanPayment = getMeanPaymentService().getComboMeanPayment();
		}
		return meanPayment;		
	}

	public void setMeanPayment(Map<String, String> meanPayment) {
		this.meanPayment = meanPayment;
	}
	public IDocumentTypeService getDocumentTypeService() {
		return documentTypeService;
	}
	
	public void setDocumentTypeService(IDocumentTypeService documentTypeService) {
		this.documentTypeService = documentTypeService;
	}
	
	public IUserService getUserService() {
		return userService;
	}
	
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	public void setTrips(Map<String, String> trips) {
		this.trips = trips;
	}
	
	public void setAddPassengerReservationManageBean(
			AddPassengerReservationManageBean addPassengerReservationManageBean) {
		this.addPassengerReservationManageBean = addPassengerReservationManageBean;
	}
	
	public void resetUser() {
		getAddPassengerReservationManageBean().reset();
	}
	
	public void saveUser() {
		RequestContext context = RequestContext.getCurrentInstance();
		String result = getAddPassengerReservationManageBean().save();
		
		if(BusinessSettings.UNSUCCESS == result) {			
			context.addCallbackParam("isValid", false); 
		} else {
			context.addCallbackParam("isValid", true); 
			setSelectedUser(getAddPassengerReservationManageBean().getUser());
			actualTab = this.TAB_CONFIRMATION;
		}
		
	}
	
	public void search() {
		userLazyDataModel = null;
	}
	
	public void reset() {
		userFilter = new UserFilter();
		userLazyDataModel = null;
	}
	
	public UserFilter getUserFilter() {
		return userFilter;
	}
	
	public void setUserFilter(UserFilter userFilter) {
		this.userFilter = userFilter;
	}
	
	public AddPassengerReservationManageBean getAddPassengerReservationManageBean() {
		return addPassengerReservationManageBean;
	}
	
	public UserLazyDataModel getUserLazyDataModel() {
		if(userLazyDataModel == null) {
			userLazyDataModel = new UserLazyDataModel(userService, userFilter);
		}
		return userLazyDataModel;
	}

	public void setUserLazyDataModel(UserLazyDataModel userLazyDataModel) {
		this.userLazyDataModel = userLazyDataModel;
	}
	
	public void setDocuments(Map<String, String> documents) {
		this.documents = documents;
	}

	public Map<String, String> getDocuments() {
		if(documents == null) {
			documents = this.getDocumentTypeService().getComboDocuments();
		}
		return documents;
	}
	
	public Map<String, String> getDaysWeek() {
		
		Map<String, String> days = new LinkedHashMap<String, String>();
		days.put(FacesUtil.getMessage("sunday"), BusinessSettings.DAY_SUNDAY);
		days.put(FacesUtil.getMessage("monday"), BusinessSettings.DAY_MONDAY);
		days.put(FacesUtil.getMessage("tuesday"), BusinessSettings.DAY_TUESDAY);
		days.put(FacesUtil.getMessage("wednesday"), BusinessSettings.DAY_WEDNESDAY);
		days.put(FacesUtil.getMessage("thursday"), BusinessSettings.DAY_THURSDAY);
		days.put(FacesUtil.getMessage("friday"), BusinessSettings.DAY_FRIDAY);
		days.put(FacesUtil.getMessage("saturday"), BusinessSettings.DAY_SATURDAY);
		
		return days;
	}
	
	public Boolean validateTravelInformation() {
		RequestContext context = RequestContext.getCurrentInstance();
		Boolean isValid = Boolean.TRUE;
		
		if(getFixedReservationBean().getFinishDate().before(getFixedReservationBean().getStartDate())) {
			FacesUtil.showFatalMessage("fixedReservationForm", "dateRangeWrong");
			isValid = Boolean.FALSE;
		}
		
		if(getFixedReservationBean().getSelectedDays().isEmpty()) {
			FacesUtil.showFatalMessage("fixedReservationForm", "requiredDayWeek");
			isValid = Boolean.FALSE;
		}
		
		context.addCallbackParam("isValid", isValid);
		return isValid;
	}
	
	public void validateAvailability() {
		
		if(this.validateTravelInformation()) {
			this.getFixedReservationService().validateAvailability(getFixedReservationBean(), "fixedReservationForm", Boolean.TRUE);
		}
	}
	
	public IFixedReservationService getFixedReservationService() {
		return fixedReservationService;
	}
	
	public void setFixedReservationService(
			IFixedReservationService fixedReservationService) {
		this.fixedReservationService = fixedReservationService;
	}
	
	public String generateReservation() {
		this.getFixedReservationService().generateReservation(getFixedReservationBean(), getSelectedUser());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_USER, getSelectedUser());
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FIX_RESERVATION, getFixedReservationBean());
		return BusinessSettings.SUCCESS;
	}
	
	public String onFlowProcess(FlowEvent event) {  
		
		if(event.getOldStep().equals(this.TAB_TRAVEL_INFORMATION)) {
			actualTab = this.TAB_USER;
			if(skipUser) {
				actualTab = this.TAB_CONFIRMATION;
				return this.TAB_CONFIRMATION;
			}
		} 
		
		return event.getNewStep();
    }
	
	public String skipStep() {
		if(actualTab.equals(this.TAB_TRAVEL_INFORMATION) && skipSchedule) {
			return this.TAB_USER;
		}
		
		return actualTab;
	}
	
	public void editUser() {
		this.getEditPassengerReservationManageBean().setPassanger(getUserLazyDataModel().getRowData());
	}

	public EditPassengerReservationManageBean getEditPassengerReservationManageBean() {
		return editPassengerReservationManageBean;
	}

	public void setEditPassengerReservationManageBean(
			EditPassengerReservationManageBean editPassengerManageBean) {
		this.editPassengerReservationManageBean = editPassengerManageBean;
	}
	
	public void saveChangesUser() {
		RequestContext context = RequestContext.getCurrentInstance();
		String result = getEditPassengerReservationManageBean().save();
		
		if(BusinessSettings.UNSUCCESS == result) {			
			context.addCallbackParam("isValid", false); 
		} else {
			context.addCallbackParam("isValid", true); 
			setSelectedUser(getEditPassengerReservationManageBean().getUser());
			actualTab = this.TAB_CONFIRMATION;
		}	
	}
}
